//
//  Refreshable.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/3/1.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import MJRefresh
import RxSwift
import RxCocoa


enum LSFRefreshStatus {
    case none
    case beginHeaderRefresh
    case endHeaderRefresh
    case beginFooterRefresh
    case endFooterRefresh
    case noMoreData
}

/* ============================ OutputRefreshProtocol ================================ */
// viewModel 中 output使用

protocol LSFRefreshProtocol {
    ///告诉外界scrollView当前的刷新状态
    var refreshStatus: Variable<LSFRefreshStatus> {get}
}

extension LSFRefreshProtocol {
    func autoSetRefreshStatus(header: MJRefreshHeader?, footer: MJRefreshFooter?) -> Disposable {
        return refreshStatus.asObservable().subscribe(onNext: { (status) in
            switch status {
            case .beginHeaderRefresh:
                header?.beginRefreshing()
                
            case .endHeaderRefresh:
                header?.endRefreshing()
                
            case .beginFooterRefresh:
                footer?.beginRefreshing()
                
            case .endFooterRefresh:
                footer?.endRefreshing()
                
            case .noMoreData:
                footer?.endRefreshingWithNoMoreData()
                
            default:
                break
            }
        })
    }
}


/* ============================ Refreshable ================================ */
// 需要使用 MJExtension 的控制器使用
private var scrollViewKey = "scrollViewKey"

///上拉刷新、下拉加载
protocol Refreshable {}

// MARK:-
// MARK: UIViewController
extension Refreshable where Self: UIViewController {
   
    
    //    下拉刷新控件
    func initRefreshHeader(_ scrollView:UIScrollView,
                           refreshingBlock: @escaping () -> Void ) {
        _setScrollView(scrollView)
        let header = MJRefreshNormalHeader.init(refreshingBlock: refreshingBlock)
        header?.lastUpdatedTimeLabel.isHidden = true
        header?.stateLabel.isHidden = true
        scrollView.mj_header = header
    }
    
    //    上拉加载控件
    func initRefreshFooter(_ scrollView:UIScrollView,
                           refreshingBlock: @escaping ()->Void ) {
        _setScrollView(scrollView)
        let footer = MJRefreshBackNormalFooter.init(refreshingBlock: refreshingBlock)
        scrollView.mj_footer = footer
    }
    
    //    上拉加载，下拉刷新
    func initRefresh(_ scrollView:UIScrollView,
                     headerBlock: @escaping ()->Void,
                     footerBlock: @escaping ()->Void ) {
        
        initRefreshHeader(scrollView, refreshingBlock: headerBlock)
        initRefreshFooter(scrollView, refreshingBlock: footerBlock)
    }
    
    
    func endRefreshing()  {
        let scrollView = _refreshScrollView()
        if let header = scrollView?.mj_header {
            header.endRefreshing()
        }
        
        if let footer = scrollView?.mj_footer {
            footer.endRefreshing()
        }
    }
    
    //    判断是否正在刷新
    func isRefreshing() -> Bool {
        let scrollView = _refreshScrollView()
        if let header = scrollView?.mj_header {
            if header.isRefreshing {
                return true
            }
        }
        
        if let footer = scrollView?.mj_footer {
            if footer.isRefreshing {
                return true
            }
        }
        
        return false
    }
    
    
    
    private func _setScrollView(_ scrollView: UIScrollView) {
        objc_setAssociatedObject(self, &scrollViewKey, scrollView, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }
    
    private func _refreshScrollView() -> UIScrollView? {
        return objc_getAssociatedObject(self, &scrollViewKey) as? UIScrollView
    }
}


// MARK:-
// MARK: UIScrollView
extension Refreshable where Self: UIScrollView {
    //    下拉刷新控件
    func initRefreshHeader(_ refreshingBlock: @escaping () -> Void ) {
        let header = MJRefreshNormalHeader.init(refreshingBlock: refreshingBlock)
        header?.lastUpdatedTimeLabel.isHidden = true
        header?.stateLabel.isHidden = true
        self.mj_header = header
    }
    
    //    上拉加载控件
    func initRefreshFooter(_ refreshingBlock: @escaping ()->Void ) {
        let footer = MJRefreshBackNormalFooter.init(refreshingBlock: refreshingBlock)
        self.mj_footer = footer
    }
    
    //    上拉加载，下拉刷新
    func initRefresh(_ scrollView:UIScrollView,
                     headerBlock: @escaping ()->Void,
                     footerBlock: @escaping ()->Void ) {
        
        initRefreshHeader(headerBlock)
        initRefreshFooter(footerBlock)
    }
    
    func endRefreshing()  {
        if let header = self.mj_header {
            header.endRefreshing()
        }
        
        if let footer = self.mj_footer {
            footer.endRefreshing()
        }
    }
    
    //    判断是否正在刷新
    func isRefreshing() -> Bool {
        if let header = self.mj_header {
            if header.isRefreshing {
                return true
            }
        }
        
        if let footer = self.mj_footer {
            if footer.isRefreshing {
                return true
            }
        }
        
        return false
    }
}
